package SMSGUI;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public class uitExcel
{
	public static void toFile(HashMap<Integer, String[]> temp)
	{
		
		HashMap<String, int[]> provincielijst = new HashMap<String, int[]>();
		try 
		{
			Iterator<Integer> k = temp.keySet().iterator();
			int d;
			String p;
			double counter = 0;
			double totaalSentiment = 0;
			String provincie;
			String sentiment;
			int oldsentiment;
			int newsentiment;
			int sentiment2;
			int dinges = 0;
			int dinges2 = 0;
			double mean = 0;
			
			
			//while loop om provincies te classificeren
			while(k.hasNext())
			{ 
				d = k.next();
				provincie = temp.get(d)[0];
				sentiment = temp.get(d)[1];
				sentiment2 = Integer.parseInt(sentiment);
				int[] a = new int[2];
				a[0] = 0;
				a[1] = 0; 	
				
				if(provincie.isEmpty())
				{
					provincie = "unknown";
				}
				
				if(provincielijst.containsKey(provincie))
				{ 	
					oldsentiment = provincielijst.get(provincie)[0];
					dinges = provincielijst.get(provincie)[1];
					newsentiment = oldsentiment + sentiment2;
					a[0] = newsentiment;
					dinges2 = dinges + 1;
					a[1] = dinges2;
					provincielijst.put(provincie, a);
				}
				else
				{					
					a[0] = sentiment2;
					a[1] = 1;
					provincielijst.put(provincie, a);
					
				}
				
				
			
			}

			//-------------------------------------------------------------------------

			Iterator<String> z = provincielijst.keySet().iterator();
			
			BufferedWriter out = new BufferedWriter(new FileWriter(
					"SentimentMap.txt"));

			while(z.hasNext())
			{
				p = z.next();
				int[] p1 = provincielijst.get(p);
				totaalSentiment = p1[0];
				counter = p1[1];
				mean = (totaalSentiment/counter);
				
				mean *=  100;
				double mean2 = (int)mean;
				mean2 /= 100;
				out.write(p + " " + mean2+ "\n");
 			}
			out.close();
		}
		
		
			catch (IOException e) 
			{
				e.printStackTrace();
				System.err.println("Unable to read ");
			}		

		//-------------------------------------------------------------------------
		
		
	}


	
}	